package com.example.chuntiao.cophook.mytools;

import com.example.chuntiao.cophook.beans.BaseDataSupport;
import com.example.chuntiao.cophook.beans.Choice;
import com.example.chuntiao.cophook.beans.ChoiceDataSupport;
import com.example.chuntiao.cophook.beans.Org;
import com.example.chuntiao.cophook.beans.OrgDataSupport;
import com.example.chuntiao.cophook.beans.Person;
import com.example.chuntiao.cophook.beans.PersonDataSupport;
import com.example.chuntiao.cophook.beans.PersonHideDataSupport;
import com.github.promeg.pinyinhelper.Pinyin;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by chuntiao on 17-7-29.
 */

public class ArrPolisher {


    private static boolean containsAndSavePerson(Person person, List<PersonDataSupport> list) {

        if (list.size() == 0)
            return false;
        for (BaseDataSupport t : list) {
            if (person.equals(t)) {
                t.set(person);
                return t.save();
            }
        }
        return false;
    }

    public static boolean containsAndSaveOrg(Org org, List<OrgDataSupport> list) {
        if (list.size() == 0)
            return false;
        for (BaseDataSupport t : list) {
            if (org.equals(t)) {
                t.set(org);
                return t.save();
            }
        }
        return false;
    }

    public static boolean containsAndSaveChoice(Choice choice, List<ChoiceDataSupport> list) {
        if (list.size() == 0)
            return false;
        for (BaseDataSupport t : list) {
            if (choice.equals(t)) {
                return true;
            }
        }
        return false;
    }

    private static boolean containsAndSaveHided(Person person, List<PersonHideDataSupport> list) {

        if (list.size() == 0)
            return false;
        for (BaseDataSupport t : list) {
            if (person.equals(t)) {
                t.set(person);
                return t.save();
            }
        }
        return false;
    }

    public static void checkContentAndSaveOrg(ArrayList<Org> orgToPolish) {
        List<OrgDataSupport> orgLocal = DataSupport.findAll(OrgDataSupport.class);

        for (OrgDataSupport o : orgLocal//清理多余数据
                ) {
            if (!orgToPolish.contains(o))
                o.delete();
        }
        orgLocal.clear();
        orgLocal = DataSupport.findAll(OrgDataSupport.class);

        for (Org o : orgToPolish
                ) {
            if (!containsAndSaveOrg(o, orgLocal))//本地包含则update否则插入
                new OrgDataSupport(o).save();
        }

    }

    public static void checkContentAndSaveChoice(ArrayList<Choice> orgToPolish) {
        List<ChoiceDataSupport> choiceDataSupports = DataSupport.findAll(ChoiceDataSupport.class);

        for (ChoiceDataSupport c : choiceDataSupports//清理多余数据
                ) {
            if (!orgToPolish.contains(c))
                c.delete();
        }
        choiceDataSupports.clear();
        choiceDataSupports = DataSupport.findAll(ChoiceDataSupport.class);

        for (Choice o : orgToPolish
                ) {
            if (!containsAndSaveChoice(o, choiceDataSupports))
                new ChoiceDataSupport(o).save();
        }
    }


    public static void devideCheckRepeatPersonsAndSave(ArrayList<Person> personListToPolish) {
        List<PersonDataSupport> personsLocal = DataSupport.findAll(PersonDataSupport.class);//保存的数据
        List<PersonHideDataSupport> personHided = DataSupport.findAll(PersonHideDataSupport.class);
        LinkedList<Person> tPersons = new LinkedList<>();

        for (PersonDataSupport p : personsLocal
                ) {
            if (!personListToPolish.contains(p)) {//删除本地多余数据
                p.delete();
            }
        }
        for (PersonHideDataSupport p : personHided
                ) {
            if (!personListToPolish.contains(p)) {//删除本地多余数据
                p.delete();
            }
        }

        personsLocal.clear();
        personHided.clear();
        personsLocal = DataSupport.findAll(PersonDataSupport.class);//删除后刷新
        personHided = DataSupport.findAll(PersonHideDataSupport.class);

        for (Person p : personListToPolish
                ) {
            if (personHided.contains(p)) {
                tPersons.add(p);
            }
        }

        for (Person p : tPersons
                ) {
            personListToPolish.remove(p);//将数据分类
            if (!containsAndSaveHided(p, personHided))
                new PersonHideDataSupport(p).save();//更新隐藏数据
        }
        tPersons.clear();

        for (Person p : personListToPolish
                ) {
            if (!containsAndSavePerson(p, personsLocal))
                new PersonDataSupport(p).save();
        }


    }

    public static void polish(ArrayList<Person> personListToPolish) {
        final boolean flags[] = new boolean[26];

        for (Person p : personListToPolish) {

            if (p.getPyOfHomeName() == null || p.getPyOfHomeName().equals("")) {//没拼音时设置拼音

                if (Pinyin.isChinese(p.getName().charAt(0)))
                    p.setPyOfHomeName(Pinyin.toPinyin(p.getName().charAt(0)));
                else if (
                        p.getName().charAt(0) >= 'a' && p.getName().charAt(0) <= 'z'
                                || p.getName().charAt(0) >= 'A' && p.getName().charAt(0) <= 'Z'
                        )
                    p.setPyOfHomeName((p.getName().charAt(0) + "").toUpperCase());
                else
                    p.setPyOfHomeName("Z");
            }
            flags[p.getPyOfHomeName().charAt(0) - 'A'] = true;
        }
        for (int i = 0; i < 26; i++) {
            if (flags[i]) {
                Person tPerson = new Person();
                tPerson.setPyOfHomeName("" + (char) (i + (int) 'A'));
                personListToPolish.add(tPerson);
            }
        }
    }

    public static void polishAndSave(ArrayList<Person> personListToPolish) {


        devideCheckRepeatPersonsAndSave(personListToPolish);
        /***
         * 加入ABCD分组*/
        polish(personListToPolish);
    }
}
